package com.star.spring.ratelimit.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 异步日志测试控制器
 * 
 * @author star
 * @since 2025-01-24
 */
@Slf4j
@RestController
@RequestMapping("/api/async-log-test")
public class AsyncLogTestController {

    /**
     * 测试异步日志功能
     */
    @GetMapping("/test")
    public Map<String, Object> testAsyncLog() {
        long startTime = System.currentTimeMillis();
        
        Map<String, Object> result = new HashMap<>();
        result.put("message", "异步日志测试成功");
        result.put("timestamp", System.currentTimeMillis());
        result.put("thread", Thread.currentThread().getName());
        
        long endTime = System.currentTimeMillis();
        result.put("responseTime", endTime - startTime);
        
        log.info("异步日志测试 - 主线程: {}, 响应时间: {}ms", 
                Thread.currentThread().getName(), endTime - startTime);
        
        return result;
    }

    /**
     * 批量测试异步日志性能
     */
    @GetMapping("/batch-test")
    public Map<String, Object> batchTestAsyncLog() {
        long startTime = System.currentTimeMillis();
        
        // 模拟一些处理逻辑
        try {
            Thread.sleep(10); // 模拟10ms的处理时间
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("message", "批量异步日志测试");
        result.put("batchSize", 1);
        result.put("timestamp", System.currentTimeMillis());
        result.put("thread", Thread.currentThread().getName());
        
        long endTime = System.currentTimeMillis();
        result.put("responseTime", endTime - startTime);
        
        log.info("批量异步日志测试 - 主线程: {}, 响应时间: {}ms", 
                Thread.currentThread().getName(), endTime - startTime);
        
        return result;
    }
}
